//
//  CommentsTableViewCell.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/18.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "CommentsTableViewCell.h"
#import "LuBDefine.h"

#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

@implementation CommentsTableViewCell

- (void)dealloc{
    [_comments release];
    [_avatarImageView release];
    [_disnameLabel release];
    [_contentLabel release];
    [_timeLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCommentsTableViewCell];
    }
    return self;
}

- (void)createCommentsTableViewCell{
    self.selectionStyle = 0;
    
    self.avatarImageView = [[UIImageView alloc]init];
//    self.avatarImageView.backgroundColor = [UIColor redColor];
    [self addSubview:_avatarImageView];
    [_avatarImageView release];
    
    self.disnameLabel = [[UILabel alloc]init];
//    self.disnameLabel.backgroundColor = [UIColor purpleColor];
    [self addSubview:_disnameLabel];
    [_disnameLabel release];
    
    self.contentLabel = [[UILabel alloc]init];
//    self.contentLabel.backgroundColor = [UIColor yellowColor];
    [self addSubview:_contentLabel];
    [_contentLabel release];
    
    self.timeLabel = [[UILabel alloc]init];
//    self.timeLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:_timeLabel];
    [_timeLabel release];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.avatarImageView.frame = CGRectMake(20, 10, CWIDTH / 9, CWIDTH / 9);
    self.avatarImageView.layer.shadowOffset = CGSizeMake(5, 5);
    self.avatarImageView.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.avatarImageView.layer.shadowOpacity = 1;
    self.avatarImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.avatarImageView.layer.borderWidth = 2;
    
    self.disnameLabel.frame = CGRectMake(20 * 2 + self.avatarImageView.frame.size.height, 10, WIDTH / 3, 20);
    self.disnameLabel.textColor = [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha:1.0];
    self.disnameLabel.font = [UIFont systemFontOfSize:14];
    
    self.contentLabel.frame = CGRectMake(20 * 2 + self.avatarImageView.frame.size.height, 20 + self.disnameLabel.frame.size.height, WIDTH - self.avatarImageView.frame.size.width - 20 * 3 , 20);
    self.contentLabel.textColor = [UIColor grayColor];
    self.contentLabel.font = [UIFont systemFontOfSize: 12];
    
    self.timeLabel.frame = CGRectMake(WIDTH - WIDTH / 4, 10, WIDTH / 4 - 20, 20);
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.timeLabel.font = [UIFont systemFontOfSize:12];
}

- (void)setComments:(Comments *)comments{
    if (_comments != comments) {
        [_comments release];
        _comments = [comments retain];
    }
    NSURL *avatarUrl = [NSURL URLWithString:comments.avatar];
    [self.avatarImageView sd_setImageWithURL:avatarUrl placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    self.disnameLabel.text = comments.disname;
    self.timeLabel.text = comments.time;
    self.contentLabel.text = comments.content;
}

+ (CommentsTableViewCell *)commentsTableViewWithTableView:(UITableView *)tableView{
    static NSString *commentsCell = @"commentCell";
    CommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:commentsCell];
    if (!cell) {
        cell = [[[CommentsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commentsCell] autorelease];
    }
    return cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
